package com.xgq.drink.controller;

import com.xgq.drink.annotation.RequiresLogin;
import com.xgq.drink.entity.R;
import com.xgq.drink.service.IChatGroupService;
import com.xgq.drink.service.IChatMessageService;
import com.xgq.drink.utils.JwtTokenUtil;
import com.xgq.drink.utils.RUtils;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

import java.util.List;
import java.util.Map;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 聊天控制器
 */
@RestController
@RequestMapping("/chat")
@Tag(name = "聊天控制器", description = "提供聊天相关接口")
public class ChatController {

    @Autowired
    private IChatMessageService chatMessageService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private IChatGroupService chatGroupService;

    @GetMapping("/private/history")
    @Operation(summary = "获取私聊历史消息")
    public R getPrivateChatHistory(@RequestParam String receiverId,
                                   @RequestParam int page,
                                   @RequestParam int pageSize, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        String uId = jwtTokenUtil.getUserIdFromToken(token);
        Long brandId = jwtTokenUtil.getStoreIdFromToken(token);
        if (brandId != null) {
            uId = "1000"+brandId;
        }
        System.out.println("senderId:" + uId);
        List<Map<String, Object>> messages = chatMessageService.fetchPaginatedMessages(receiverId, uId, page,
                pageSize);
        return RUtils.success("获取私聊历史消息成功", messages);
    }

    /**
     * 获取群聊历史消息
     */
    @GetMapping("/group/history")
    @Operation(summary = "获取群聊历史消息")
    public R getGroupChatHistory(@RequestParam String groupId,
            @RequestParam int page,
            @RequestParam int pageSize) {
        List<Map<String, Object>> messages = chatMessageService.fetchPaginatedMessages(groupId, null, page, pageSize);
        return RUtils.success("获取群聊历史消息成功", messages);
    }

    /**
     * 标记消息为已读
     */
    @RequiresLogin
    @PostMapping("/message/read")
    @Operation(summary = "标记消息为已读")
    public R markMessageAsRead(@RequestParam String messageId) {
        return chatMessageService.markMessageAsRead(messageId);
    }

    /**
     * 标记所有未读消息为已读
     */
    @RequiresLogin
    @PostMapping("/message/read/all")
    @Operation(summary = "标记所有未读消息为已读")
    public R markAllMessagesAsRead(@RequestParam String receiverId, @RequestParam String senderId) {
        return chatMessageService.markAllMessagesAsRead(receiverId, senderId);
    }

    /**
     * 创建聊天群组
     */
    @RequiresLogin
    @PostMapping("/group/create")
    @Operation(summary = "创建聊天群组")
    public R createChatGroup(@RequestParam String ownerId,
            @RequestParam String groupName,
            @RequestParam(required = false) String groupAvatar,
            @RequestParam(required = false) String announcement) {
        return chatGroupService.createChatGroup(ownerId, groupName, groupAvatar, announcement);
    }

    /**
     * 更新群组信息
     */
    @RequiresLogin
    @PutMapping("/group/update")
    @Operation(summary = "更新群组信息")
    public R updateChatGroup(@RequestParam String groupId,
            @RequestParam(required = false) String groupName,
            @RequestParam(required = false) String groupAvatar,
            @RequestParam(required = false) String announcement,
            @RequestParam String userId) {
        return chatGroupService.updateChatGroup(groupId, groupName, groupAvatar, announcement, userId);
    }

    /**
     * 解散群组
     */
    @RequiresLogin
    @DeleteMapping("/group/dismiss")
    @Operation(summary = "解散群组")
    public R dismissChatGroup(@RequestParam String groupId, @RequestParam String userId) {
        return chatGroupService.dismissChatGroup(groupId, userId);
    }

    /**
     * 获取用户群组列表
     */
    @RequiresLogin
    @GetMapping("/group/list")
    @Operation(summary = "获取用户群组列表")
    public R getUserChatGroups(@RequestParam String userId) {
        return chatGroupService.getUserChatGroups(userId);
    }

    /**
     * 获取群组详情
     */
    @RequiresLogin
    @GetMapping("/group/detail")
    @Operation(summary = "获取群组详情")
    public R getChatGroupDetail(@RequestParam String groupId) {
        return chatGroupService.getChatGroupDetail(groupId);
    }

    /**
     * 添加群组成员
     */
    @RequiresLogin
    @PostMapping("/group/member/add")
    @Operation(summary = "添加群组成员")
    public R addGroupMember(@RequestParam String groupId,
            @RequestParam String userId,
            @RequestParam(required = false) Integer role,
            @RequestParam String operatorId) {
        return chatGroupService.addGroupMember(groupId, userId, role, operatorId);
    }

    /**
     * 移除群组成员
     */
    @RequiresLogin
    @DeleteMapping("/group/member/remove")
    @Operation(summary = "移除群组成员")
    public R removeGroupMember(@RequestParam String groupId,
            @RequestParam String userId,
            @RequestParam String operatorId) {
        return chatGroupService.removeGroupMember(groupId, userId, operatorId);
    }

    /**
     * 更新成员角色
     */
    @RequiresLogin
    @PutMapping("/group/member/role")
    @Operation(summary = "更新成员角色")
    public R updateMemberRole(@RequestParam String groupId,
            @RequestParam String userId,
            @RequestParam Integer role,
            @RequestParam String operatorId) {
        return chatGroupService.updateMemberRole(groupId, userId, role, operatorId);
    }

    /**
     * 获取群组成员列表
     */
    @RequiresLogin
    @GetMapping("/group/member/list")
    @Operation(summary = "获取群组成员列表")
    public R getGroupMembers(@RequestParam String groupId) {
        return chatGroupService.getGroupMembers(groupId);
    }

    /**
     * 退出群组
     */
    @RequiresLogin
    @PostMapping("/group/quit")
    @Operation(summary = "退出群组")
    public R quitGroup(@RequestParam String groupId, @RequestParam String userId) {
        return chatGroupService.quitGroup(groupId, userId);
    }
}